1
'****************************** Module Header ******************************\
2 ' Module Name: LinqToString.vb
3 ' Project: VBLinqExtension
4 ' Copyright (c) Microsoft Corporation.
6 ' It is a simple LINQ to String library to show the digis characters in a
7 ' string, to count occurrences of a word in a string, and to query for
8 ' sentences that contain a specified set of words.
10 ' This source is subject to the Microsoft Public License.
11 ' See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
12 ' All other rights reserved.
14 ' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
15 ' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
16 ' WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
17 '***************************************************************************/
19 #Region
"Imports directives"
21 Imports System
.Collections
.Generic
24 Imports System
.Runtime
.CompilerServices
28 Public Module LinqToString
31 ''' Extension method of string to get the digit characters
34 ''' <returns>IEnumerable collection digit characters</returns>
37 Public Function GetDigits(ByVal text
As String) As IEnumerable(Of Char
)
38 ' Get the chars if it is a digit
39 Dim digits
= From ch
In text _
40 Where Char
.IsDigit(ch
) _
48 ''' Extension method of string to count occurrences of a word
51 ''' <param name="searchTerm">The search item string</param>
52 ''' <returns>The search item occurences</returns>
55 Public Function GetWordOccurrence(ByVal text
As String, ByVal searchTerm
As String) As Integer
56 ' Split the source string into single words
57 Dim source
As String() = text
.Split(New Char() {"."c
, "?"c
, "!"c
, " "c
, ";"c
, ":"c
, _
58 ","c
}, StringSplitOptions
.RemoveEmptyEntries)
60 ' Query the occurences of the search item
61 Dim matchQueryCount
As Integer = (From word
In source _
62 Where word
.ToLowerInvariant() = searchTerm
.ToLowerInvariant() _
65 Return (matchQueryCount
)
69 ''' Extension method of string to query for sentences that contain
70 ''' a specified set of words.
72 ''' <param name="wordsToMatch">The search item set</param>
73 ''' <returns>The set of sentences</returns>
76 Public Function GetCertainSentences(ByVal text
As String, ByVal wordsToMatch
As String()) As IEnumerable(Of
String)
77 ' Split the source string into single sentences
78 Dim sentences
= text
.Split(New Char() {"."c
, "?"c
, "!"c
})
80 ' Query for sentences that contain the set of search itmes
81 Dim sentenceQuery
= From sentence
In sentences _
82 Let w
= sentence
.Split(New Char() {"."c
, "?"c
, "!"c
, " "c
, ";"c
, ":"c
, _
83 ","c
}, StringSplitOptions
.RemoveEmptyEntries) _
84 Where w
.Distinct().Intersect(wordsToMatch
).Count() = wordsToMatch
.Count() _